package com.freshcity.page.tree
{
	import com.freshcity.ILayer;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.tree.XinYuanWin;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class XinYuanGuo extends Window
	{
		public var tree:MyTree;
		
		private var back:XinYuanWin;
		public function XinYuanGuo(parent:ILayer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			
			this.mouseEnabled = false;
			this.visible = false;
			this.title = "许一个心愿:";
			this.hasCloseButton = true;
			this.setSize(380,220);
			
			back.cancelBtn.addEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK,submit);
		}
		
		protected function submit(event:MouseEvent):void
		{
			var len:int=StringUtil.getANSILength(back.selfDesc.text);
			if (len <= 5)
			{
				main.msg.showRollTipsMsg("心愿描述太简单啦");
				return;
			}
			
			back.submitBtn.enabled = false;
			var c:String = StringUtil.trim(back.selfDesc.text);
			main.commu.send(OpCodes.WRITE_XINYUAN, {c:c}, xinyuanCallback);
			main.commu.register(OpCodes.INIT_QINGYI,initQingYi);
		}
		
		private function initQingYi(data:*):void
		{
			var qing:QingYiGuo = new QingYiGuo();
			qing.main = this.main;
			qing.data = data;
			qing.show();
		}
		
		private function xinyuanCallback(data:*):void
		{
			main.msg.showFixRollMsg("成功许下一个愿望！");
			main.msg.showPopUpMsg("心愿已成功许下，从现在起，你将开始接受祝福...",MsgManager.HAHA,0,MsgManager.BOTTOM_RIGHT);
			tree.setTreeData(data);
			this.hide();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new XinYuanWin();
			addChild(back);
		}
		
		override public function destroy():void
		{
			back.cancelBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK,submit);
			super.destroy();
		}
		
		override public function hide(event:Event=null):void
		{
			TweenMax.to(this,0.3,{autoAlpha:0,y:"+30",onComplete:destroy});
			EffectManager.hideUIMask();
		}
		
		override public function show(event:Event=null):void
		{
			if(!this.parent){
				this.layer = LayerManager.popupLayer;
				this.layer.addPopUp(this);
			}
			this.x = (stage.stageWidth-back.width)/2;
			this.y = (stage.stageHeight-back.height)/2 - 30;
			
			EffectManager.addUIMask([],0,true);
			TweenMax.to(this,0.3,{autoAlpha:1,y:"+30"});
		}
		
		
	}
}